import java.awt.*;
import java.util.List;

public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //    链表的属性  链表的头节点
    public ListNode head;

    public void createList(){
        ListNode list1=new ListNode(12);
        ListNode list2=new ListNode(23);
        ListNode list3=new ListNode(34);
        ListNode list4=new ListNode(45);
        list1.next=list2;
        list2.next=list3;
        list3.next=list4;
        this.head=list1;

    }

    @Override
    public void display() {
        ListNode cur=head;
       while(cur!=null){
           System.out.print(cur.val+" ");
           cur=cur.next;

       }
    }

    @Override
    public boolean contains(int key) {
        //        求链表是否包括key节点
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
                cur=cur.next;
        }
        return false;
    }

    @Override
    public int size() {
        ListNode cur=head;
        int size=0;
//        求链表的节点
        while(cur!=null){
            size++;
            cur=cur.next;
        }
        return size;
    }

    @Override
    public void addFirst(int data) {
//      头插法
        ListNode node=new ListNode(data);
        if(head==null){
            this.head=node;
        }else{
            node.next=this.head;
            this.head=node;
        }
    }

    @Override
    public void addLast(int data) {
//        尾插法
        ListNode node=new ListNode(data);
        if(head == null){
           this.head=node;
        }else {
            ListNode cur=head;
            while(cur!=null){
              cur=cur.next;
            }
            cur.next=node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
//       添加一个节点
        if(index< 0 || index > size() ){
//            自定义异常
            throw new InvalidIndexException("Invalid index: "+ index);
        } else if (index==0) {
            addFirst(data);
        } else if (index==size()) {
            addLast(data);
            return;
        }
        ListNode cur = searchPrev(index);
        ListNode node =new ListNode(data);
        node.next=cur.next;
        cur.next=node;
    }
    private ListNode searchPrev(int index){
        ListNode cur =this.head;
        int count=0;
        while(count!=index-1){
            cur=cur.next;
            count++;
        }
        return cur;
    }
    @Override
    public void remove(int key) {
        // 处理头结点是要删除的情况
        if (this.head != null && this.head.val == key) {
            this.head = this.head.next;
            return;
        }

        ListNode cur = this.head;

        // 找到key
        while (cur != null && cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }
    private ListNode findPrev(int key){
//        找到关键字key的前一个节点的地址
        ListNode cur=this.head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
        }
        return null;
    }
    @Override
    public void removeAllKey(int key) {
//        删除所有值为key的节点
        if(this.head==null){
            return;
        }
        ListNode prev=head;
        ListNode cur=head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else {
                prev=cur;
                cur=cur.next;
            }
        }
        if (head.val==key){
            head=head.next;
        }
    }



    @Override
    public void clear() {
//        清屏---> 把每一个节点值,next域都为null
        ListNode cur=head;
        while(cur!=null){
            ListNode curNext=cur.next;
            cur.next=null;
            cur=curNext;
        }
        head=null;
    }
    public ListNode reverseList(){
        if (head==null){
            return null;
        }
//        只有一个节点
        if(head.next==null){
            return head;
        }
        ListNode cur=head.next;
        head.next=null;
        while(cur!=null){
            ListNode curNext=cur.next;
            cur.next=head;
            head=cur;
            cur=curNext;
        }
        return head;
    }
    public ListNode findKthFromEnd(int k){
        // 求链表中倒数k个节点
//        快指针先走k-1步然后再一起走 最后返回慢指针
        if(k > size() || k <= 0){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k - 1; i++) {
            if (fast == null) {
                return null;
            }
            fast = fast.next;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    public boolean chkPalindrome(ListNode A) {
        if (head==null) ||head.next==null){
            return true;
        }
//        判断链表是否为回文结构
        //找到中间位置  -->快慢指针找中间节点
        // 从中间位置之后反转 判断是否回文

//        1.找中间位置
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null && fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
//      2.翻转
        ListNode cur=slow;
        while(cur!=null){
            ListNode curNext=cur.next;
            cur.next=slow;
            slow=cur;
            cur=curNext;
        }
//        3.从前往后 从后往前
        while(head!= slow){
            if(head.val!=slow.val){
                return false;
            }
            if(head.next==slow){
                return true;
            }
            head=head.next;
            slow=slow.next;
        }
        return true;
    }
    public boolean hasCycle(ListNode head) {
//        判断链表是否成环
        if(head==null || head.next==null){
            return false;
        }
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null && fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                return true;
            }
        }
        return false;
    }
}
